unit clsdevcompra;

interface
uses Dm, dbtables, biblio, Sysutils;
type
  TDevCompra = class(tobject)
  private
    Devc_Numero: string;
    Devc_Serie: string;
    Ent_Codigo: string;
  public
    Emp_Codigo: string;
    Codf_Codigo: string;
    Nato_Codigo: string;
    Moe_Codigo: string;
    Hist_codigo: string;
    Notc_numero: string;
    Notc_Serie: string;
    Notc_numpgtos: integer;
    Notc_observacao: string;
    Notc_aliqipi: double;
    Notc_aliqicms: double;
    Notc_aliqicmssubs: double;
    Notc_descicms: double;
    Notc_aliqdesconto: double;
    Notc_aliqencargos: double;
    Notc_valorfrete: double;
    Notc_valorseguro: double;
    Notc_valorencargos: double;
    Notc_bcalcipi: double;
    Notc_bcalcicms: double;
    Notc_bcalcicmssubs: double;
    Notc_bcalcdescicms: double;
    Notc_dataentrada: TDateTime;
    Notc_datafatura: TDateTime;
    Notc_valoripi: double;
    Notc_valoricms: double;
    Notc_valoricmssubs: double;
    Notc_valordescicms: double;
    Notc_isentaipi: double;
    Notc_isentaicms: double;
    Notc_outrasipi: double;
    Notc_outrasicms: double;
    Notc_valorcontabil: double;
    Notc_inscestsub: string;
    Notc_Desconto: double;
    function Gravar(p_Devc_Numero, p_Devc_Serie, p_Ent_codigo: string): Byte;
    function Recuperar(p_Devc_Numero, p_Devc_Serie, p_Ent_codigo: string): Byte;
    function Excluir(p_Devc_Numero, p_Devc_Serie, p_Ent_codigo: string): Byte;

    //nao foi arrumado ainda
    //procedure Consultar(var p_q:TQuery; p_desc:Tdatetime);
    //procedure Consultar_Cliente(var p_q:TQuery; p_Ped_numero:String);
  end;

implementation

{ TDevCompra }

function TDevCompra.Excluir(p_Devc_Numero, p_Devc_Serie,
  p_Ent_codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt.Db);
  tq.Name := 'qDevCompra';
  tq.DatabaseName := tq.DatabaseName;
  tq.SQL.Clear;
  tq.Params.Clear;
  tq.sql.Add('Delete from DevCompra where Devc_Numero=:K_Devc_Numero and Devc_Serie=:K_Devc_Serie and ent_codigo=:K_ent_codigo');
  tq.ParamByName('K_Devc_Numero').AsString := p_Devc_Numero;
  tq.ParamByName('K_Devc_Serie').AsString := p_Devc_Serie;
  tq.ParamByName('K_ent_codigo').AsString := StrZero(p_Ent_codigo, 5);
  try
    tq.ExecSQL;
    Result := 1;
  except
    Result := 0;
  end;
  tq.close;
  tq.free;

end;

function TDevCompra.Gravar(p_Devc_Numero, p_Devc_Serie,
  p_Ent_codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt.Db);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  //   tq.Name:='QDevCompra';
  tq.Active := false;
  tq.SQL.clear;
  tq.Params.Clear;
  tq.SQL.add('Select * from DevCompra where Devc_Numero=:K_Devc_Numero and Devc_Serie=:K_Devc_Serie and ent_codigo=:k_ent_codigo');
  tq.ParamByName('K_Devc_Numero').AsString := P_Devc_Numero;
  tq.ParamByName('K_Devc_Serie').AsString := P_Devc_Serie;
  tq.ParamByName('K_ent_codigo').AsString := StrZero(p_ent_codigo, 5);
  tq.Open;
  if tq.eof then begin
    tq.SQL.Clear;
    tq.Params.clear;
    tq.SQL.add('Insert into DevCompra values(:K_Devc_Numero,' +
      ':K_Devc_Serie,' +
      ':K_Ent_codigo,' +
      ':K_Emp_Codigo,' +
      ':K_Codf_Codigo,' +
      ':K_Nato_Codigo,' +
      ':K_Moe_Codigo,' +
      ':K_Hist_codigo,' +
      ':k_notc_numero,' +
      ':k_notc_serie,' +
      ':K_Notc_numpgtos,' +
      ':K_Notc_observacao,' +
      ':K_Notc_aliqipi,' +
      ':K_Notc_aliqicms,' +
      ':K_Notc_aliqicmssubs,' +
      ':K_Notc_descicms,' +
      ':K_Notc_aliqdesconto,' +
      ':K_Notc_aliqencargos,' +
      ':K_Notc_valorfrete,' +
      ':K_Notc_valorseguro,' +
      ':K_Notc_valorencargos,' +
      ':K_Notc_bcalcipi,' +
      ':K_Notc_bcalcicms,' +
      ':K_Notc_bcalcicmssubs,' +
      ':K_Notc_bcalcdescicms,' +
      ':K_Notc_dataentrada,' +
      ':K_Notc_datafatura,' +
      ':K_Notc_valoripi,' +
      ':K_Notc_valoricms,' +
      ':K_Notc_valoricmssubs,' +
      ':K_Notc_valordescicms,' +
      ':K_Notc_isentaipi,' +
      ':K_Notc_isentaicms,' +
      ':K_Notc_outrasipi,' +
      ':K_Notc_outrasicms,' +
      ':K_Notc_valorcontabil,' +
      ':K_Notc_inscestsub,' +
      ':k_Notc_Desconto)');
  end
  else begin
    tq.SQL.Clear;
    tq.Params.clear;
    tq.sql.add('Update DevCompra set ' +
      'Emp_Codigo=:K_emp_codigo,' +
      'Codf_Codigo=:K_codf_codigo,' +
      'Nato_Codigo=:K_nato_codigo,' +
      'Moe_Codigo=:K_moe_codigo,' +
      'Hist_codigo=:K_Hist_codigo,' +
      'Notc_Numero=:k_notc_numero,' +
      'notc_serie=:k_notc_serie,' +
      'Notc_numpgtos=:K_Notc_numpgtos,' +
      'Notc_observacao=:K_Notc_observacao,' +
      'Notc_aliqipi=:K_Notc_aliqipi,' +
      'Notc_aliqicms=:K_Notc_aliqicms,' +
      'Notc_aliqicmssubs=:K_Notc_aliqicmssubs,' +
      'Notc_descicms=:K_Notc_descicms,' +
      'Notc_aliqdesconto=:K_Notc_aliqdesconto,' +
      'Notc_aliqencargos=:K_Notc_aliqencargos,' +
      'Notc_valorfrete=:K_Notc_valorfrete,' +
      'Notc_valorseguro=:K_Notc_valorseguro,' +
      'Notc_valorencargos=:K_Notc_valorencargos,' +
      'Notc_bcalcipi=:K_Notc_bcalcipi,' +
      'Notc_bcalcicms=:K_Notc_bcalcicms,' +
      'Notc_bcalcicmssubs=:K_Notc_bcalcicmssubs,' +
      'Notc_bcalcdescicms=:K_Notc_bcalcdescicms,' +
      'Notc_dataentrada=:K_Notc_dataentrada,' +
      'Notc_datafatura=:K_Notc_datafatura,' +
      'Notc_valoripi=:K_Notc_valoripi,' +
      'Notc_valoricms=:K_Notc_valoricms,' +
      'Notc_valoricmssubs=:K_Notc_valoricmssubs,' +
      'Notc_valordescicms=:K_Notc_valordescicms,' +
      'Notc_isentaipi=:K_Notc_isentaipi,' +
      'Notc_isentaicms=:K_Notc_isentaicms,' +
      'Notc_outrasipi=:K_Notc_outrasipi,' +
      'Notc_outrasicms=:K_Notc_outrasicms,' +
      'Notc_valorcontabil=:K_Notc_valorcontabil,' +
      'Notc_inscestsubs=:K_Notc_inscestsub,' +
      'Notc_Desconto=:k_Notc_Desconto ' +
      'where Devc_Numero=:K_Devc_Numero and Devc_Serie =:K_Devc_Serie and Ent_codigo=:K_ent_codigo');
  end;
  tq.parambyname('K_Devc_Numero').asstring := p_Devc_Numero;
  tq.parambyname('K_Devc_Serie').asstring := p_Devc_Serie;
  tq.parambyname('K_Ent_codigo').asstring := p_Ent_codigo;
  tq.parambyname('K_emp_codigo').asstring := Emp_Codigo;
  tq.parambyname('K_codf_codigo').asstring := Codf_Codigo;
  tq.parambyname('K_nato_codigo').asstring := Nato_Codigo;
  tq.parambyname('K_moe_codigo').asstring := Moe_Codigo;
  tq.parambyname('K_Hist_codigo').asstring := Hist_codigo;
  tq.parambyname('k_notc_numero').asstring := notc_numero;
  tq.parambyname('k_notc_serie').asstring := notc_serie;
  tq.parambyname('K_Notc_numpgtos').AsInteger := Notc_numpgtos;
  tq.parambyname('K_Notc_observacao').asstring := Notc_observacao;
  tq.parambyname('K_Notc_aliqipi').AsFloat := Notc_aliqipi;
  tq.parambyname('K_Notc_aliqicms').AsFloat := Notc_aliqicms;
  tq.parambyname('K_Notc_aliqicmssubs').AsFloat := Notc_aliqicmssubs;
  tq.parambyname('K_Notc_descicms').AsFloat := Notc_descicms;
  tq.parambyname('K_Notc_aliqdesconto').AsFloat := Notc_aliqdesconto;
  tq.parambyname('K_Notc_aliqencargos').AsFloat := Notc_aliqencargos;
  tq.parambyname('K_Notc_valorfrete').AsFloat := Notc_valorfrete;
  tq.parambyname('K_Notc_valorseguro').AsFloat := Notc_valorseguro;
  tq.parambyname('K_Notc_valorencargos').AsFloat := Notc_valorencargos;
  tq.parambyname('K_Notc_bcalcipi').AsFloat := Notc_bcalcipi;
  tq.parambyname('K_Notc_bcalcicms').AsFloat := Notc_bcalcicms;
  tq.parambyname('K_Notc_bcalcicmssubs').AsFloat := Notc_bcalcicmssubs;
  tq.parambyname('K_Notc_bcalcdescicms').AsFloat := Notc_bcalcdescicms;
  tq.parambyname('K_Notc_dataentrada').AsDateTime := Notc_dataentrada;
  tq.parambyname('K_Notc_datafatura').AsDateTime := Notc_datafatura;
  tq.parambyname('K_Notc_valoripi').AsFloat := Notc_valoripi;
  tq.parambyname('K_Notc_valoricms').AsFloat := Notc_valoricms;
  tq.parambyname('K_Notc_valoricmssubs').AsFloat := Notc_valoricmssubs;
  tq.parambyname('K_Notc_valordescicms').AsFloat := Notc_valordescicms;
  tq.parambyname('K_Notc_isentaipi').AsFloat := Notc_isentaipi;
  tq.parambyname('K_Notc_isentaicms').AsFloat := Notc_isentaicms;
  tq.parambyname('K_Notc_outrasipi').AsFloat := Notc_outrasipi;
  tq.parambyname('K_Notc_outrasicms').AsFloat := Notc_outrasicms;
  tq.parambyname('K_Notc_valorcontabil').AsFloat := Notc_valorcontabil;
  tq.parambyname('K_Notc_inscestsub').AsString := Notc_inscestsub;
  tq.parambyname('K_Notc_desconto').AsFloat := Notc_desconto;
  tq.Close;
  try
    tq.execsql;
    result := 1;
  except
    result := 0;
    tq.Free;
  end;
  tq.Free;
end;

function TDevCompra.Recuperar(p_Devc_Numero, p_Devc_Serie,
  p_Ent_codigo: string): Byte;
var
  tq: tquery;
begin
  tq := TQuery.create(DmInt.Db);
  tq.name := 'QDevCompra';
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.sql.clear;
  tq.Params.clear;
  tq.SQL.add('Select * from DevCompra where Devc_Numero=:K_Devc_Numero and Devc_Serie=:K_Devc_Serie and Ent_codigo=:k_Ent_codigo');
  tq.Params[0].asstring := p_Devc_Numero;
  tq.Params[1].asstring := p_Devc_Serie;
  tq.Params[2].asstring := StrZero(p_Ent_codigo, 5);
  tq.Open;
  if tq.Eof then begin
    Devc_Numero := '';
    Devc_Serie := '';
    Ent_Codigo := '';
    Emp_Codigo := '';
    Codf_Codigo := '';
    Nato_Codigo := '';
    Moe_Codigo := '';
    Hist_codigo := '';
    notc_numero := '';
    notc_serie := '';
    Notc_numpgtos := 0;
    Notc_observacao := '';
    Notc_aliqipi := 0;
    Notc_aliqicms := 0;
    Notc_aliqicmssubs := 0;
    Notc_descicms := 0;
    Notc_aliqdesconto := 0;
    Notc_aliqencargos := 0;
    Notc_valorfrete := 0;
    Notc_valorseguro := 0;
    Notc_valorencargos := 0;
    Notc_bcalcipi := 0;
    Notc_bcalcicms := 0;
    Notc_bcalcicmssubs := 0;
    Notc_bcalcdescicms := 0;
    Notc_dataentrada := Date;
    Notc_datafatura := Date;
    Notc_valoripi := 0;
    Notc_valoricms := 0;
    Notc_valoricmssubs := 0;
    Notc_valordescicms := 0;
    Notc_isentaipi := 0;
    Notc_isentaicms := 0;
    Notc_outrasipi := 0;
    Notc_outrasicms := 0;
    Notc_valorcontabil := 0;
    Notc_inscestsub := '';
    Notc_Desconto := 0;
    Result := 0;
  end
  else begin
    Devc_Numero := p_Devc_Numero;
    Devc_Serie := p_Devc_Serie;
    Ent_Codigo := p_Ent_codigo;
    Emp_Codigo := tq.fieldbyname('emp_codigo').asstring;
    Codf_Codigo := tq.fieldbyname('codf_codigo').asstring;
    Nato_Codigo := tq.fieldbyname('nato_codigo').asstring;
    Moe_Codigo := tq.fieldbyname('moe_codigo').asstring;
    Hist_codigo := tq.fieldbyname('Hist_codigo').asstring;
    notc_numero := tq.fieldbyname('notc_numero').asstring;
    notc_serie := tq.fieldbyname('notc_serie').asstring;
    Notc_numpgtos := tq.fieldbyname('Notc_numpgtos').AsInteger;
    Notc_observacao := tq.fieldbyname('Notc_observacao').asstring;
    Notc_aliqipi := tq.fieldbyname('Notc_aliqipi').AsFloat;
    Notc_aliqicms := tq.fieldbyname('Notc_aliqicms').AsFloat;
    Notc_aliqicmssubs := tq.fieldbyname('Notc_aliqicmssubs').AsFloat;
    Notc_descicms := tq.fieldbyname('Notc_descicms').AsFloat;
    Notc_aliqdesconto := tq.fieldbyname('Notc_aliqdesconto').AsFloat;
    Notc_aliqencargos := tq.fieldbyname('Notc_aliqencargos').AsFloat;
    Notc_valorfrete := tq.fieldbyname('Notc_valorfrete').AsFloat;
    Notc_valorseguro := tq.fieldbyname('Notc_valorseguro').AsFloat;
    Notc_valorencargos := tq.fieldbyname('Notc_valorencargos').AsFloat;
    Notc_bcalcipi := tq.fieldbyname('Notc_bcalcipi').AsFloat;
    Notc_bcalcicms := tq.fieldbyname('Notc_bcalcicms').AsFloat;
    Notc_bcalcicmssubs := tq.fieldbyname('Notc_bcalcicmssubs').AsFloat;
    Notc_bcalcdescicms := tq.fieldbyname('Notc_bcalcdescicms').AsFloat;
    Notc_dataentrada := tq.fieldbyname('Notc_dataentrada').AsDateTime;
    Notc_datafatura := tq.fieldbyname('Notc_datafatura').AsDateTime;
    Notc_valoripi := tq.fieldbyname('Notc_valoripi').AsFloat;
    Notc_valoricms := tq.fieldbyname('Notc_valoricms').AsFloat;
    Notc_valoricmssubs := tq.fieldbyname('Notc_valoricmssubs').AsFloat;
    Notc_valordescicms := tq.fieldbyname('Notc_valordescicms').AsFloat;
    Notc_isentaipi := tq.fieldbyname('Notc_isentaipi').AsFloat;
    Notc_isentaicms := tq.fieldbyname('Notc_isentaicms').AsFloat;
    Notc_outrasipi := tq.fieldbyname('Notc_outrasipi').AsFloat;
    Notc_outrasicms := tq.fieldbyname('Notc_outrasicms').AsFloat;
    Notc_valorcontabil := tq.fieldbyname('Notc_valorcontabil').AsFloat;
    Notc_inscestsub := tq.fieldbyname('Notc_inscestsubs').AsString;
    Notc_desconto := tq.fieldbyname('Notc_desconto').AsFloat;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

end.
